layui.use(['layer','form'],function () {
    var $ = layui.jquery;
    var layer=layui.layer;
    var form=layui.form;
    //自定义验证规则
    form.verify({
        title: function(value){
            if(value.length < 3){
                return "用户名至少3个字符";
            }
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                return '用户名不能有特殊字符';
            }
            if(/^\d+\d+\d$/.test(value)){
                return '用户名不能全为数字';
            }
        }
        ,pass:function (value) {
            if (value.length>0)
            {
                if (!(/^[\S]{6,12}$/.test(value)))
                {
                    return '密码必须6到12位';
                }
            }
        }
        ,content: function(value){
            layedit.sync(editIndex);
        }
    });
    $(function () {
        userNameChang(); //实时验证用户名是否存在
        var userid= parent.window.userid;
        //信息预加载
        $.ajax({
            type:'post',
            url:'/Ticket/userInfo/InfoLoad',
            dataType:'json',
            data:{'userid':userid},
            success:function (res) {
                if (res.code==0)
                {
                    var data=res.data[0];
                    var src="../img/"+data.head;
                    document.getElementById('headImgUp').src=src;
                    $('#username').val(data.username);
                    // $('#password').val("");
                    $('#phone').val(data.phone);
                    $('#papers').val(data.papers);
                    $('#remarks').val(data.remarks);
                }
                else layer.ms(res.msg);
            }
        });
    });
    form.on('submit(submit)', function(data){
        $.ajax({
            type:'post',
            url:'/Ticket/userInfo/updateInfoCode',
            dataType: 'json',
            data:{'id':parent.window.userid},
            success:function (res) {
                if(res.code==0)
                {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        layer.prompt({
                                title:'请输入验证码'},
                            function(value, index, elem){
                                $.ajax({
                                    type:'post',
                                    url:'/Ticket/userInfo/updateInfo',
                                    dataType: 'json',
                                    data:{'id':parent.window.userid,'mobileCode':value,'username':data.field.username,'password':data.field.password,'phone':data.field.phone,
                                        'papers':data.field.papers,'remarks':data.field.remarks},
                                    success:function (res) {
                                        if(res.code==0)
                                        {
                                            layer.msg(res.msg);
                                            setTimeout(function () {
                                                location.reload();
                                            },2000)
                                        }
                                        else layer.msg(res.msg);
                                    }
                                });
                                layer.close(index);
                            });
                    },2000)
                }
                else layer.msg(res.msg);
            }
        });
        return false;
    });
});
//实时验证用户名是否重复
function userNameChang() {
    var username=document.getElementById('username');
    if("\v"=="v")//验证是否是ie
    {
        layer.msg("不支持的浏览器");
    }
    else {
        username.addEventListener("input",function (event) {
            var name=username.value;
            $.ajax({
                type:'post',
                url:'/Ticket/home/userNameChange?time='+new Date().getTime(),
                dataType:'json',
                data:{'value':name},
                success:function (res) {
                    if(res.code!=0)
                    {
                        $('#usernameUp').text("该用户名已被使用");
                    }
                    else $('#usernameUp').text("");
                }
            })
        });
    }
};
